<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flags Manager</title>
<!-- <link href="../css/bootstrap.css" rel="stylesheet" type="text/css"> -->
<link href="../css/bootstrap-3.3.4.css" rel="stylesheet" type="text/css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>
<div class="container">
    <?php
    include_once("../core/database.php");
    if(!db_flag_exist())
    {
        echo "
            <div class=\"row\">
                <div class=\"col-sm-4 col-lg-offset-4 center-block\">
                    <button type=\"button\" class=\"btn btn-default center-block\" onclick=\"location.href='setup.php'\">Database is currpted, click to setup.</button>
                </div>
            </div>";
        exit();
    }
    ?>
    <div class="row">
        <div class="col-sm-4 col-sm-offset-4">
            <button type="button" class="btn btn-default center-block" onclick="location.href='flags.php?action=showall&start=0&number=20'">Show all flags</button>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-6">
            <div class="input-group">
              <span class="input-group-addon">Server : </span>
              <input type="text" class="form-control" placeholder="Server address. eg : 192.168.1.1">
            </div>
        </div>
        <div class="col-sm-4"><button type="button" class="btn btn-default">search all flags for server</button></div>
    </div>
    <div class="row"> &nbsp;</div>
<?php
include_once("../core/database.php");
function showall()
{
    $start = intval($_REQUEST['start']);
    $number = intval($_REQUEST['number']);
    $flags = flags_query($start, $number);
    
    // output navigator at first line
    $prevstart = max($start - 20, 0);
    $nextstart = $start + $number;
    $firstLink = "<a href='flags.php?action=showall&start=0&number=20'>First</a>";
    $prevLink = "<a href='flags.php?action=showall&start=$prevstart&number=20'>Prev</a>";
    $nextLink = "<a href='flags.php?action=showall&start=$nextstart&number=20'>Next</a>";
    if($start == 0)
        $firstLink =  $prevLink = "";
    if(sizeof($flags) < $number)
        $nextLink = "";
    echo <<<str
    <div class="row text-center">
        <div class="col-sm-2 col-sm-offset-2">$firstLink</div>
        <div class="col-sm-2">$prevLink</div>
        <div class="col-sm-2">$nextLink</div>
    </div>
str;
    
    // output table headers
    echo <<<str
    <div class="row">
        <div class="col-sm-1 text-center"> Id </div>
        <div class="col-sm-2 text-center"> Server </div>
        <div class="col-sm-4 text-center"> Flag </div>
        <div class="col-sm-2 text-center"> Datetime </div>
        <div class="col-sm-3 text-center"> Operation </div>
    </div>
str;
    
    // output flags
    while(list(, $flag) = each($flags))
    {
        echo <<<str
    <div class="row text-center">
        <div class="col-sm-1 text-center"> $flag->id </div>
        <div class="col-sm-2 text-center"> $flag->server </div>
        <div class="col-sm-4 text-center"> $flag->flag </div>
        <div class="col-sm-2 text-center"> $flag->datetime </div>
        <div class="col-sm-3 text-center"> 
            <a href="flags.php?action=delete&flag=$flag->flag&start=$start&number=$number">Del</a>
        </div>
    </div>
str;
    }
    
}
function delete()
{
    $flag = strval($_REQUEST['flag']);
    flags_delete($flag);
    showall();
}
$actions = array('showall'=>1, 'delete'=>1);
if(isset($_REQUEST['action']) && isset($actions[strval($_REQUEST['action'])]))
    call_user_func(strval($_REQUEST['action']));
?>
</div>
</body>
</html>
